package com.jpemartin.jgt;

import java.awt.*;
import java.awt.font.GlyphVector;

/** Draw the given text with the given color. Typically you'd only use the ctor
 *  and the draw(g,x,y) methods. 
 *  
 *  This version does the same thing as DrawText, but it tries to do it faster by caching
 *  the intermediate glyphs. Testing, however, shows that this is not faster at all
 *  (see DrawTextBench). */
public class DrawTextGlyph implements Drawable {

   protected String _text;
   protected Color _color;
   protected GlyphVector _cachedText;
   
   /** Initialize the object with a given text and color */
   public DrawTextGlyph(String text, Color color) {
      if (null==text || null==color) throw new NullPointerException("DrawText does not accept null parameters");
      _text = text;
      _cachedText = null;
      _color = color;
   }
   
   /** @return the color of the text */
  public Color getColor() {
     return _color;
  }

  /** Set the text color */
  public void setColor(Color _color) {
     this._color = _color;
  }
  
   /** draw the text at the given position, using the default font */
   public void draw(Graphics g, int x, int y) {
      Graphics2D g2 = (Graphics2D) g;
      if (null==_cachedText) {
         Font f = g.getFont();
         _cachedText = f.createGlyphVector(g2.getFontRenderContext(), _text);
      }
      g.setColor(_color);
      g2.drawGlyphVector(_cachedText,x,y);
   }
   

}
